package com.chuiyan.topnews;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import java.util.ArrayList;
import java.util.List;

public class video extends Fragment {
    private RecyclerView recyclerView;

    private VideoRecyclerAdapter mAdapter;

    private FrameLayout videoRootViewFl;

    private MyVideoView videoView;

    private FrameLayout fullScreen;

    private View lastView;

    private int videoPosition = -1;

    private List<VideoBean> videoBeanList = new ArrayList<>();

    private int[] imageIds = new int[]{R.drawable.hzw_a, R.drawable.hzw_b,
            R.drawable.hzw_d, R.drawable.hzw_e, R.drawable.hzw_f, R.drawable.hzw_h,
            R.drawable.hzw_i, R.drawable.hzw_j, R.drawable.hzw_k};
    //后添加的图
    private static String[] paths = {
            "/home/lennon/video/longzhuhunjian.mp4",
            "http://upos-hz-mirrorks3u.acgvideo.com/upgcxcode/37/73/60857337/60857337-1-6.mp4?e=ig8euxZM2rNcNbuBhwdVtWuBhwdVNEVEuCIv29hEn0l5QK==&deadline=1559564456&gen=playurl&nbs=1&oi=3745628122&os=ks3u&platform=html5&trid=ea8dd0fdf5f54ee3b421d5c4a9c7f631&uipk=5&upsig=36bac65fa5685c05aaa024f72ad38cb8&uparams=e,deadline,gen,nbs,oi,os,platform,trid,uipk&mid=0",
            "http://upos-hz-mirrorks3u.acgvideo.com/upgcxcode/15/54/91775415/91775415-1-6.mp4?e=ig8euxZM2rNcNbuVhwdVtWuVhwdVNEVEuCIv29hEn0l5QK==&deadline=1559564505&gen=playurl&nbs=1&oi=3745628122&os=ks3u&platform=html5&trid=d14c9194858043a3b373f687efb019f0&uipk=5&upsig=49bb9ce0c6ab85ea4db631a1c8d82aa8&uparams=e,deadline,gen,nbs,oi,os,platform,trid,uipk&mid=0",
            "http://upos-hz-mirrorkodou.acgvideo.com/upgcxcode/67/92/93449267/93449267-1-6.mp4?e=ig8euxZM2rNcNbuVhwdVtWuVhwdVNEVEuCIv29hEn0l5QK==&deadline=1559564554&gen=playurl&nbs=1&oi=3745628122&os=kodou&platform=html5&trid=c9ae42a9c62748d8aaca0682e57775ad&uipk=5&upsig=9c8b5661cc19d665f83abf1db88c5eb1&uparams=e,deadline,gen,nbs,oi,os,platform,trid,uipk&mid=0",
            "http://upos-hz-mirrorks3u.acgvideo.com/upgcxcode/52/16/95241652/95241652-1-6.mp4?e=ig8euxZM2rNcNbuBhwdVtWuBhwdVNEVEuCIv29hEn0l5QK==&deadline=1559564600&gen=playurl&nbs=1&oi=3745628122&os=ks3u&platform=html5&trid=a2f5cb64196245c1b50b7fd1976a6dfe&uipk=5&upsig=f0a8b8e17d6a7aae6943f9e556de9878&uparams=e,deadline,gen,nbs,oi,os,platform,trid,uipk&mid=0"
    };
    //    private ArrayList<String> paths = new ArrayList<String>();
    private static String VIDEO_PATH = "http://upos-hz-mirrorks3u.acgvideo.com/upgcxcode/33/86/59208633/59208633-1-6.mp4?e=ig8euxZM2rNcNbug7WdVtWug7WdVNEVEuCIv29hEn0l5QK==&deadline=1559564102&gen=playurl&nbs=1&oi=3745628122&os=ks3u&platform=html5&trid=c0ab66579f114c3694d34e4457d199a7&uipk=5&upsig=5f097ee57596bdc2ab5ba62d627415d8&uparams=e,deadline,gen,nbs,oi,os,platform,trid,uipk&mid=0";

    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.video, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initData();
        initView();
        initEvent();
    }

    private void initView() {
        recyclerView = getView().findViewById(R.id.recyclerView);
        videoRootViewFl = getView().findViewById(R.id.video_root_fl);
        fullScreen = getView().findViewById(R.id.video_full_screen);
        mAdapter = new VideoRecyclerAdapter(videoBeanList);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        recyclerView.setAdapter(mAdapter);
    }

    private void showVideo(View view, final String videoPath) {
        View v;
        removeVideoView();
        if (videoRootViewFl.getVisibility() == View.VISIBLE) {
            videoRootViewFl.removeAllViews();
            videoRootViewFl.setVisibility(View.GONE);
        }
        if (videoView == null) {
            videoView = new MyVideoView(getContext());

            videoView.setListener(new MyVideoView.IFullScreenListener() {
                @Override
                public void onClickFull(boolean isFull) {
                    Intent intent = new Intent(getActivity(), Detail_video.class);
                    startActivity(intent);
                }
            });
        }
        videoView.stop();
        v = view.findViewById(R.id.item_imageview);
        if (v != null) v.setVisibility(View.INVISIBLE);
        v = view.findViewById(R.id.item_image_play);
        if (v != null) v.setVisibility(View.INVISIBLE);
        v = view.findViewById(R.id.item_video_root_fl);
        if (v != null) {
            v.setVisibility(View.VISIBLE);
            FrameLayout fl = (FrameLayout) v;
            fl.removeAllViews();
            fl.addView(videoView, new ViewGroup.LayoutParams(-1, -1));
            VIDEO_PATH = videoPath;
            videoView.setVideoPath(videoPath);
            videoView.start();
        }
        lastView = view;
    }

    private void removeVideoView() {
        View v;
        if (lastView != null) {
            v = lastView.findViewById(R.id.item_imageview);
            if (v != null) v.setVisibility(View.VISIBLE);
            v = lastView.findViewById(R.id.item_image_play);
            if (v != null) v.setVisibility(View.VISIBLE);
            v = lastView.findViewById(R.id.item_video_root_fl);
            if (v != null) {
                FrameLayout ll = (FrameLayout) v;
                ll.removeAllViews();
                v.setVisibility(View.GONE);
            }
        }
    }

    /*
     * videoBeanList实例化*/
    private void initData() {
        VideoBean videoBean;
        for (int i = 0; i < 50; i++) {
//            videoBean = new VideoBean(imageIds[i % imageIds.length],VIDEO_PATH);
            videoBean = new VideoBean(imageIds[i % imageIds.length],paths[i%5]);
            videoBeanList.add(videoBean);
        }
    }

    private void initEvent() {
        mAdapter.setListener(new VideoRecyclerAdapter.OnClickPlayListener() {
            @Override
            public void onPlayClick(View view, String videoPath) {
                showVideo(view, videoPath);
            }
        });
        recyclerView.addOnChildAttachStateChangeListener(new RecyclerView.OnChildAttachStateChangeListener() {
            @Override
            public void onChildViewAttachedToWindow(View view) {
                if (videoPosition == -1 || videoRootViewFl.getVisibility() != View.VISIBLE) {
                    return;
                }
                if (videoPosition == recyclerView.getChildAdapterPosition(view)) {
                    videoPosition = -1;
                    showVideo(view, VIDEO_PATH);
                }
            }

            @Override
            public void onChildViewDetachedFromWindow(View view) {
                if (videoView == null || videoRootViewFl.getVisibility() == View.VISIBLE) return;
                View v = view.findViewById(R.id.item_video_root_fl);
                if (v != null) {
                    FrameLayout fl = (FrameLayout) v;
                    videoPosition = recyclerView.getChildAdapterPosition(view);
                    if (fl.getChildCount() > 0) {
                        fl.removeAllViews();
                        int position = 0;
                        if (videoView.isPlaying()) {
                            position = videoView.getPosition();
                            videoView.stop();
                        }
                        videoRootViewFl.setVisibility(View.VISIBLE);
                        videoRootViewFl.removeAllViews();
                        lastView = videoRootViewFl;
                        videoRootViewFl.addView(videoView, new ViewGroup.LayoutParams(-1, -1));
                        videoView.setVideoPath(VIDEO_PATH);
                        videoView.start();
                        videoView.seekTo(position);
//                        if (videoView.isPause()) {
//                            videoView.resume();
//                        }
                    }
                    fl.setVisibility(View.GONE);
                }
                v = view.findViewById(R.id.item_imageview);
                if (v != null) {
                    if (v.getVisibility() != View.VISIBLE) {
                        v.setVisibility(View.VISIBLE);
                    }
                }
                v = view.findViewById(R.id.item_image_play);
                if (v != null) {
                    if (v.getVisibility() != View.VISIBLE) {
                        v.setVisibility(View.VISIBLE);
                    }
                }
            }
        });
    }

    @Override
    public void onDestroy() {
        if (videoView != null) {
            videoView.stop();
        }
        super.onDestroy();
    }
}
